<template>
	<div class="system-dic-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="769px">
			<el-form ref="fileDialogFormRef" :model="state.ruleForm" size="default" label-width="90px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="文件选择" v-if="state.dialog.type === 'add'">
							<el-upload ref="upload" v-model:file="state.file" class="upload-demo" :auto-upload="false"
								:on-change="fileChange" :limit="1" :on-exceed="handleExceed" accept=".jpeg,.jpg,.png">
								<el-button type="primary">点击选择文件</el-button>
								<template #tip>
									<div class="el-upload__tip">
										所选图片小于1M
									</div>
								</template>
							</el-upload>
						</el-form-item>
					</el-col>
					<el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="文件名">
							<el-input v-model="state.ruleForm.filename" placeholder="请输入文件名" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="文件状态">
							<el-switch v-model="state.ruleForm.status" inline-prompt active-text="启" :active-value="0"
								inactive-text="禁" :inactive-value="0"></el-switch>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="文件描述">
							<el-input v-model="state.ruleForm.description" type="textarea" placeholder="请输入文件描述"
								maxlength="150"></el-input>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="systemDicDialog">
import { nextTick, reactive, ref } from 'vue';
import { ElMessage, UploadFile, UploadFiles, UploadInstance, UploadProps, UploadRawFile, genFileId } from 'element-plus';
import { useFileApi } from '/@/api/file';
// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);

const fileApi = useFileApi()
const upload = ref<UploadInstance>()

// 定义变量内容
const fileDialogFormRef = ref();
const state = reactive({
	ruleForm: {
		id: 0,
		status: 0,
		filename: "",
		description: ""
	},
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
	imageUrl: "",
	file: {} as File
});

// 打开弹窗
const openDialog = (type: string, row: RowFileType) => {
	state.dialog.type = type;
	if (type === 'edit') {
		state.ruleForm = row;
		state.dialog.title = '修改文件';
		state.dialog.submitTxt = '修 改';
	} else {
		state.ruleForm = { id: 0, description: "", status: 0, filename: "" } as RowFileType;
		state.file = {} as File;
		state.dialog.title = '新增文件';
		state.dialog.submitTxt = '新 增';
		nextTick(() => {
			upload.value!.clearFiles(['fail', 'ready', 'success', 'uploading']);
		})
	}
	state.dialog.isShowDialog = true;
};
// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
	closeDialog();
};
// 提交
const onSubmit = async () => {
	if (state.dialog.type === 'add') {
		let formData = new FormData();
		formData.append("info", JSON.stringify(state.ruleForm))
		formData.append("file", state.file)
		await fileApi.addFile(formData).then(res => {
			ElMessage.success("新增成功");
		}).catch(err => {
			ElMessage.warning("新增失败");
		});
	} else {
		await fileApi.updateFile(state.ruleForm).then(res => {
			ElMessage.success("修改成功");
		}).catch(err => {
			ElMessage.warning("修改失败");
		});
	}
	closeDialog();
	emit('refresh');
};

// 覆盖前一个文件
const handleExceed: UploadProps['onExceed'] = (files) => {
	upload.value!.clearFiles();
	const file = files[0] as UploadRawFile;
	file.uid = genFileId();
	upload.value!.handleStart(file);
}

// 所选文件变化
const fileChange: UploadProps['onChange'] = (uploadFile: UploadFile, uploadFiles: UploadFiles) => {
	state.file = uploadFile.raw as File;
}

// 暴露变量
defineExpose({
	openDialog,
});
</script>
